package com.miao.hong.fang.util.pagination;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import com.miao.hong.fang.util.ConfigProperties;
import com.miao.hong.fang.util.EvalHelper;

public class NavigationTag extends TagSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1100937913737809753L;
	private String currentPage;
	private String pageSize;
	private String totalCount;

	public String getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(String currentPage) throws JspException {
		this.currentPage = EvalHelper.evalString("currentPage", currentPage, this, pageContext);
		
	}

	public String getPageSize() {
		return pageSize;
	}

	public void setPageSize(String pageSize) throws JspException {
		this.pageSize = EvalHelper.evalString("pageSize", pageSize, this, pageContext);
	}

	public String getTotalCount() {
		return totalCount;
	}

	public void setTotalCount(String totalCount) throws JspException {
		this.totalCount = EvalHelper.evalString("totalCount", totalCount, this, pageContext);
	}

	public int doStartTag() throws JspException {
		
		int totalPages = Integer.valueOf(totalCount) / Integer.valueOf(pageSize);
		if(Integer.valueOf(totalCount) % Integer.valueOf(pageSize) > 0) {
			totalPages++;
		}
		
		JspWriter out = pageContext.getOut();

		StringBuffer sb = new StringBuffer();
		
		if(Integer.valueOf(currentPage) == 1) {
			sb.append("<span class=\"disable_pre_next_page\">");
			sb.append(ConfigProperties.getInstance().getProperties("messages-default", "last_page", "Previous"));
			sb.append("</span>");
		}else {
			sb.append("<a class=\"pre_next_page\" href=\"javascript:resubmit(");
			sb.append(Integer.valueOf(currentPage) - 1).append(")\">");
			sb.append(ConfigProperties.getInstance().getProperties("messages-default", "last_page", "Previous"));
			sb.append("</a>&nbsp;");
		}
		
		for (int i = 1; i < totalPages + 1; i++) {
			
			if(i == Integer.valueOf(currentPage)) {
				
				sb.append("<a class=\"pagination_current_page\" href=\"javascript:resubmit(");
				sb.append(i).append(")\">");
				sb.append(i);
				sb.append("</a>&nbsp;");	
				
//				sb.append("<font color=\"red\">");
//				sb.append(i);
//				sb.append("</font>&nbsp;");
			}
			else {
//				sb.append("<a class=\"pagination_link\" style=\"color:blue\" href=\"javascript:resubmit(");
				sb.append("<a class=\"pagination_link\" href=\"javascript:resubmit(");
				sb.append(i).append(")\">");
				sb.append(i);
				sb.append("</a>&nbsp;");			
			}
		}
		
		if(Integer.valueOf(currentPage) == totalPages) {
			sb.append("<span class=\"disable_pre_next_page\">");
			sb.append(ConfigProperties.getInstance().getProperties("messages-default", "next_page", "Next"));
			sb.append("</span>");
		} else {
			sb.append("<a class=\"pre_next_page\" href=\"javascript:resubmit(");
			sb.append(Integer.valueOf(currentPage) + 1).append(")\">");
			sb.append(ConfigProperties.getInstance().getProperties("messages-default", "next_page", "Next"));
			sb.append("</a>&nbsp;");
		}
		
		try {
			out.print(sb.toString());
		} catch (IOException e) {
			throw new JspException("IOException encountered while writing to JspWriter: " + e.getMessage(), e);
		}
		
		return EVAL_PAGE;
	}
	
}
